import global from "../../core/global.js";
import { showLayerByLayer, cleanTestLayer } from "./handle-layer.js";

/**
 * @description 底图瓦片加载 xyz
 * @return 瓦片影像 图层
 */
export function addImageryProvider(opt, cb) {
  Object.assign(opt, opt.imageryInfo || {})
  // 已经加载则不重复加载 改用显隐
  let needReload = showLayerByLayer(opt, 'show')
  if (!needReload) {
    cb && cb()
    return
  }
  let scene = window.viewer.scene
  let globe = scene.globe
  let rectangle = null
  opt.url = opt.url + opt.jointUrl
  opt.maximumLevel = Number(opt.maximumLevel)
  opt.minimumLevel = Number(opt.minimumLevel)
  opt.tileWidth = Number(opt.tileWidth)
  opt.tileHeight = Number(opt.tileHeight)
  if (opt.rectangle) {
    opt.rectangle = opt.rectangle.map(item => {
      return Number(item)
    })
    rectangle = SSmap.Rectangle.fromDegrees(
      opt.rectangle[0],
      opt.rectangle[1],
      opt.rectangle[2],
      opt.rectangle[3]
    )
  } else {
    rectangle = SSmap.Rectangle.fromDegrees(-180, -90, 180, 90)
  }
  let imageryProvider = new SSmap.UrlTemplateImageryProvider(
    opt.url,
    opt.useWebMercator, // useWebMercator
    opt.maximumLevel, // maximumLevel
    opt.minimumLevel, // minimumLevel
    opt.tileWidth, // tileWidth
    opt.tileHeight, // tileHeight
    true
  )
  let imageryLayer = new SSmap.ImageryLayer(
    imageryProvider,
    rectangle
  )
  globe.addImageryLayer(imageryLayer)
  openSceneFog(opt.lightFlag)
  openlightingEnabled(opt.lightFlag)
  if (opt.id && opt.id != 'temp_id_12138') {
    imageryLayer.type = 'ImageryLayer'
    global.layerList.add(imageryLayer, opt.id)
  } else {
    cleanTestLayer()
    imageryLayer.type = 'ImageryLayer'
    global.testLayer = imageryLayer
  }
  cb && cb()
  return imageryLayer
}

/**
 * @description 天地图影像加载
 * @returns 天地图
 */
export function addtiandiImageryLayer(opt, cb) {
  Object.assign(opt, opt.imageryInfo || {})
  // 已经加载则不重复加载 改用显隐
  let needReload = showLayerByLayer(opt, 'enabled')
  if (!needReload) {
    cb && cb()
    return
  }
  let imageryLayer = new SSmap.TiandituImageryLayer()
  imageryLayer.url = opt.url
  imageryLayer.useWebMercator = opt.useWebMercator || false;
  imageryLayer.tileWidth = Number(opt.tileWidth) || 256
  imageryLayer.tileHeight = Number(opt.tileHeight) || 256
  imageryLayer.minimumLevel = Number(opt.minimumLevel) || 1
  imageryLayer.maximumLevel = Number(opt.maximumLevel) || 17
  imageryLayer.hasAlphaChannel = opt.hasAlphaChannel || false;
  imageryLayer.isLabel = opt.isLabel || false
  imageryLayer.componentComplete()
  openSceneFog(opt.lightFlag)
  openlightingEnabled(opt.lightFlag)
  if (opt.id && opt.id != 'temp_id_12138') {
    imageryLayer.type = 'ImageryLayer'
    global.layerList.add(imageryLayer, opt.id)
  } else {
    cleanTestLayer()
    imageryLayer.type = 'ImageryLayer'
    global.testLayer = imageryLayer
  }
  cb && cb()
  return imageryLayer
}

/**
 * @description ArcGis影像加载
 * @returns ArcGis
 */
export function addArcGisImageryProvider(opt, cb) {
  Object.assign(opt, opt.imageryInfo || {})
  // 已经加载则不重复加载 改用显隐
  let needReload = showLayerByLayer(opt, 'show')
  if (!needReload) {
    cb && cb()
    return
  }
  let imageryLayer = window.viewer.scene.globe.addArcGisMapServerImageryLayer(opt.url)
  openSceneFog(opt.lightFlag)
  openlightingEnabled(opt.lightFlag)
  if (opt.id && opt.id != 'temp_id_12138') {
    imageryLayer.type = 'ImageryLayer'
    global.layerList.add(imageryLayer, opt.id)
  } else {
    cleanTestLayer()
    imageryLayer.type = 'ImageryLayer'
    global.testLayer = imageryLayer
  }
  cb && cb()
  return imageryLayer
}

/**
 * @description WMS/WMTS底图服务加载 不完善
 * @returns
 */
export function addWMTSImageryProvider(opt, cb) {
  Object.assign(opt, opt.imageryInfo || {})
  // 已经加载则不重复加载 改用显隐
  let needReload = showLayerByLayer(opt, 'show')
  if (!needReload) {
    cb && cb()
    return
  }
  let scene = window.viewer.scene
  let globe = scene.globe
  let imageryLayer = globe.addWmsImageryLayer(opt.url)
  openSceneFog(opt.lightFlag)
  openlightingEnabled(opt.lightFlag)
  if (opt.id && opt.id != 'temp_id_12138') {
    imageryLayer.type = 'ImageryLayer'
    global.layerList.add(imageryLayer, opt.id)
  } else {
    cleanTestLayer()
    imageryLayer.type = 'ImageryLayer'
    global.testLayer = imageryLayer
  }
  cb && cb()
  return imageryLayer
}

// 添加地形
export function setTerrainProvider(opt, cb) {
  Object.assign(opt, opt.imageryInfo || {})
  let globe = window.viewer.scene.globe
  if (!opt.isChecked && opt.id != 'temp_id_12138') {
    // 还原
    globe.setDefaultTerrain()
    global.layerList.remove(opt.id)
    cb && cb()
  } else {
    openSceneFog(opt.lightFlag)
    openlightingEnabled(opt.lightFlag)
    globe.setTerrainProviderUrl(opt.url).then(function () {
      globe.terrainProvider.requestWaterMask = opt.watermask || false// 控制地形水
      globe.opacity = opt.opacity || 1// 控制地形透明度
      let terrainProvider = globe.terrainProvider
      // 请求头
      if (terrainProvider) {
        if (opt.headername && opt.headers) {
          terrainProvider.setHeader(
            opt.headername,
            opt.headers
          )
        }
      }
    })
    if (opt.id && opt.id != 'temp_id_12138') {
      let returnopt = {
        isTerrain: true
      }
      global.layerList.add(returnopt, opt.id)
    } else {
      cleanTestLayer()
      let imageryLayer = { isTerrain: true }
      global.testLayer = imageryLayer
    }
    cb && cb()
  }
}

// 地表反光
export function openlightingEnabled(isopen) {
  window.viewer.scene.globe.lightingEnabled = isopen == undefined ? true : isopen;
}

// 地表透明
export function setGlobeOpacity(numOpacity) {
  window.viewer.scene.globe.opacity = numOpacity || 1.0
}

// 地表 雾效
export function openSceneFog(isopen) {
  window.viewer.scene.fog.enabled = isopen == undefined ? true : isopen;
}